package crm.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import crm.base.controller.BaseController;
import crm.base.util.SpringSecurityUtils;
import crm.system.entity.Agent;
import crm.system.model.LoginModel;
import crm.system.service.AgentService;

@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {
	
	@Autowired
	private AgentService agentService;
	
	@RequestMapping("/onLogin")
	public String onLogin(String errorCode) {
		
		System.out.println(errorCode);
//		System.out.println(request.getParameter("code"));
		
//		if (model != null) {
//			System.out.println("-=-=-");
//			System.out.println(model.getUsername() + ":" + model.getPassword());
//		}
		

		return "login";
	}
	
	@RequestMapping("/loginSuccess")
	public String loginSuccess() {
		Agent agent = SpringSecurityUtils.getCurrentUser();
		
		if (agent == null) {
			return "redirect:/login/onLogin?errorCode=123";
		}
		
		agent = agentService.getAgentByUserName(agent.getUserName());
		if (agent.getIsFirst() == 0) {
			return "changepassword";
		}
		
		return "loginsuccess";
	}
	
	
	@RequestMapping("/forgotpassword")
	public ModelAndView forgotpassword(LoginModel model) {
		
		
		return forword("forgotpassword", model);
	}
}
